<!-- BEGIN PAGE HEADER-->
<style>
    .ibox label {
        margin-left: 8px;
        margin-bottom: 8px;
    }

    .page-bar .pull-right {
        display: none;
    }

    td {
        text-align: center;
    }
</style>
<?php $this->load->view('partials/breadcrumb.tpl') ?>

<div class="row">
    <div class="col-xs-12">
        <!-- Begin: life time stats -->
        <div class="portlet">
            <?= form_open('', Array('class' => 'form-inline', 'role' => 'form')) ?>
            <?= search_form_input('order_no', filterValue('order_no'), '订单号', 'class="form-control input-xs"'); ?>
            <?= search_form_input('shipping_no', filterValue('shipping_no'), '运单号', 'class="form-control input-xs"'); ?>
            <?= search_form_input('warehousing', filterValue('warehousing'), '产品批次', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('shop_id', array('' => '选择账户...') + $allShops, filterValue('shop_id'), '所属账户', 'class="form-control input-xs select2me new-select"'); ?>
            <?= search_form_dropdown('purchaser_id', array('' => '选择采购人...') + $allPurchaser, filterValue('purchaser_id'), '采购人', 'class="form-control input-xs select2me new-select"'); ?>
            <?= search_form_dropdown('product_id', array('' => '选择商品...') + $allProducts, filterValue('product_id'), '选择商品', 'class="form-control input-xs select2me new-select"'); ?>
            <?= search_form_dropdown('type_mark', array('' => '选择出入库方式...') + IOSS_StaticConfig::getShippingType(), filterValue('type_mark'), '出入库方式', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('location', array('' => '选择所在仓库...') + IOSS_StaticConfig::getShippingWarehouse(), filterValue('location'), '选择所在仓库', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('status', array('' => '状态...') + MStorageRecord::getState(), filterValue('status'), '状态', 'class="form-control input-xs"'); ?>
            <div style="clear:both;height:14px;"></div>
            <?= search_form_input('record_number_perpage', filterValue('record_number_perpage'), '每页显示条数', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('record_order_type', array('' => '排序方式...') + MStorageRecord::getOrderType(), filterValue('record_order_type'), '排序', 'class="form-control input-xs"'); ?>
            <div class="input-group input-large date-picker input-daterange" data-date="" data-date-format="yyyy-mm-dd">
                <input type="text" class="form-control" name="record_create_time_begin"
                       value="<?= filterValue('record_create_time_begin') ?>" placeholder="开始时间" autoComplete="off">
                <span class="input-group-addon">to </span>
                <input type="text" class="form-control" name="record_create_time_end"
                       value="<?= filterValue('record_create_time_end') ?>" placeholder="结束时间" autoComplete="off">
            </div>

            <button type="button" class="btn default" id="reset">重置条件</button>
            <button type="submit" class="btn green about_search">搜索 <i class="fa fa-search"></i></button>

            <?= form_close() ?>
            <div class="portlet-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="table-scrollable">
                            <table class="table table-striped table-bordered table-hover">
                                <thead class="flip-content">
                                <tr>
                                    <th width="1%">ID</th>
                                    <th width="5%">订单号</th>
                                    <th width="5%">物流单号</th>
                                    <th width="8%">商品名称</th>
                                    <th width="3%">商品单价</th>
                                    <th width="3%">商品数量</th>
                                    <th width="3%">商品总计</th>
                                    <th width="4%">责任人</th>
                                    <th width="5%">采购人</th>
                                    <th width="4%">出入库方式</th>
                                    <th width="8%">商品批次</th>
                                    <th width="4%">所属仓库</th>
                                    <th width="4%">所在仓库区</th>
                                    <th width="6%">所属账户</th>
                                    <th width="6%">采购时间</th>
                                    <th width="5%">状态</th>
                                    <th width="10%">备注</th>
                                    <th width="2%">修改历史</th>
                                    <th width="12%">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php foreach ($list as $v) { ?>
                                    <tr>
                                        <td><?= $v->id ?></td>
                                        <td><?= $v->order_no ? $v->order_no : '无' ?></td>
                                        <td><?= $v->shipping_no ? $v->shipping_no : '无' ?></td>
                                        <td style="position: relative">
                                            <?= htmlspecialchars($v->product_name) . '(id:' . $v->product_id . ')' ?>
                                            <?= $v->type == 0 && $v->status == 1 && in_array($v->type_mark, [3, 4, 5, 6, 7, 8]) ? ($v->has_deliver ? '<span style="position: absolute;top: 0;right: 0;" class="label label-success"> 已发货 </span>' : '<span style="position: absolute;top: 0;right: 0;" class="label label-warning"> 待发货 </span>') : '' ?>
                                            <?= $v->type == 0 && $v->status == 1 && in_array($v->type_mark, [9, 10, 11, 12, 13]) ? ('<span style="position: absolute;top: 0;right: 0;" class="label label-primary"> 折损 </span>') : '' ?>
                                        </td>
                                        <td><?= htmlspecialchars($v->price) ?></td>
                                        <td><?= htmlspecialchars($v->quantity) ?></td>
                                        <td><?= htmlspecialchars($v->amount) ?></td>
                                        <td><?= htmlspecialchars($allPurchaser[$v->purchaser_id]) ?></td>
                                        <td><?= htmlspecialchars($v->related_person) ?></td>
                                        <td <?php if ($v->type_mark == 7): ?>class="popovers" data-container="body"
                                            data-trigger="hover" data-placement="top"
                                            data-content="<?= $v->order_no ?>"<?php endif; ?>><?= IOSS_StaticConfig::getShippingType($v->type_mark) ?></td>
                                        <td>
                                            <label class="label label-info"><?= htmlspecialchars($v->warehousing) ?></label>
                                        </td>
                                        <td><?= $v->location ? IOSS_StaticConfig::getShippingWarehouse($v->location) : '未入库' ?></td>
                                        <td><?= $v->area ? IOSS_StaticConfig::getShippingBlock($v->area) : '未入库' ?></td>
                                        <td><?= htmlspecialchars($allShops[$v->shop_id]) ?></td>
                                        <td><?= date('Y-m-d', $v->purchase_time) ?></td>

                                        <td><?= MStorageRecord::getState($v->status) ?></td>
                                        <td><?= htmlspecialchars($v->remark) ?></td>
                                        <td><button data-id="<?= $v->id ?>"
                                                    data-route="<?= $this->_thisModule . $this->_thisController . '/edit' ?>"
                                                    class="btn btn-sm blue edit-his">修改记录
                                            </button></td>
                                        <!--                                        <td></td>-->
                                        <td class="numeric text-center">
                                            <div class="btn-group btn-group-solid">

                                                <?php if ($v->type && $v->status == 1): ?>
                                                    <button data-id="<?= $v->id ?>" type="button"
                                                            class="btn btn-sm purple record-edit">编辑
                                                    </button>
                                                    <a href="javascript:void(defConfirm('<?= site_url($thisModule . $thisController . '/in_fail/' . $v->id) ?>'));"
                                                       class="btn btn-sm red ">作废</a>
                                                <?php endif; ?>
                                                <?php if ($v->type && $v->status == 2): ?>
                                                    <button data-id="<?= $v->id ?>" type="button"
                                                            class="btn btn-sm purple record-edit">编辑
                                                    </button>
                                                    <a href="javascript:void(defConfirm('<?= site_url($thisModule . $thisController . '/in_fail/' . $v->id) ?>'));"
                                                       class="btn btn-sm red ">作废</a>
                                                    <button onclick="confirmIn(<?= $v->id ?>)" class="btn btn-sm blue ">
                                                        入库
                                                    </button>
                                                <?php endif; ?>
                                                <?php if ($v->type && $v->status == 0): ?>
                                                    <span class="btn btn-sm btn-warning">已作废</span>
                                                <?php endif; ?>
                                                <?php if (!$v->type && $v->status == 1 && !$v->has_deliver): ?>
                                                    <button data-id="<?= $v->id ?>" type="button"
                                                            class="btn btn-sm purple record-edit">编辑
                                                    </button>
                                                    <a href="javascript:void(defConfirm('<?= site_url($thisModule . $thisController . '/out_fail/' . $v->id) ?>'));"
                                                       class="btn btn-sm red ">作废</a>
                                                <?php endif; ?>
                                                <?php if (!$v->type && $v->has_deliver && $v->status): ?>
                                                    <button data-id="<?= $v->id ?>" type="button"
                                                            class="btn btn-sm purple record-edit">编辑
                                                    </button>
                                                    <span class="btn btn-sm btn-success">已发货</span>
                                                <?php endif; ?>
                                                <?php if (!$v->type && !$v->status): ?>
                                                    <span class="btn btn-sm btn-warning">已作废</span>
                                                <?php endif; ?>

                                            </div>
                                        </td>
                                    </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right">
                        <?= $pagination ?>
                    </div>
                </div>
            </div>
        </div>
        <!-- End: life time stats -->
    </div>
</div>

<script>
    var editHis = $(".edit-his");
    editHis.click(function () {
        var id = this.dataset.id;
        var route = this.dataset.route;
        $.ajax({
            url: "<?= site_url('storage/warehouse/ajaxHis') ?>",
            method: "POST",
            data: {'id': id, 'route': route},
            dataType: "json",
            success: function (res) {
                console.log(res);
                var html = '';
                if (!res.length) {
                    toastr.error('暂无修改记录！', '暂无')
                } else {
                    for (var i = 0; i < res.length; i++) {
                        html += '<div class=""><p class="bg-warning" style="padding: 2px 4px;"> <span> '+res[i].user_name+' </span>于 <span style="color:#2120ff">'+res[i].create_time+'</span> <span>操作：</span><span style="color:red;">'+res[i].content+'</span><p></div>'
                    }
                    $("#log-content").html(html);
                    $('.logModal').modal('show');
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    });
    var recordEdit = $(".record-edit");
    recordEdit.click(function () {
        var record_id = this.dataset.id;
        $.ajax({
            url: "<?= site_url('storage/warehouse/ajaxDetail') ?>",
            method: "POST",
            data: {'id': record_id},
            dataType: "json",
            success: function (res) {
                document.getElementById("form_edit").action = "<?= site_url('/storage/warehouse/edit/') ?>" + '/' + record_id;
                $(".product_name").text(res.product_name);
                set_select_checked('shop-id', res.shop_id);
                set_select_checked('type_mark', res.type_mark);
                set_select_checked('location', res.location);
                set_select_checked('area', res.area);
                $("input[name='quantity']").val(res.quantity);
                $("input[name='order_no']").val(res.order_no);
                $("input[name='shipping_no']").val(res.shipping_no);
                $("input[name='amount']").val(parseFloat(res.amount));
                $("input[name='status']").val(parseFloat(res.status));
                $("input[name='purchase_time']").val(res.purchase_time);
                $("textarea[name='remark']").val(res.remark);
                if (res.type == 0 && res.has_deliver == 1) {
                    $(".deliver-need").fadeIn();
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
        $(".editModal").modal('show');
    });

    function set_select_checked(selectId, checkValue) {
        var select = document.getElementById(selectId);
        for (var i = 0; i < select.options.length; i++) {
            if (select.options[i].value == checkValue) {
                select.options[i].selected = true;
                break;
            }
        }
    }
</script>

<!--  log-modal  -->
<div class="modal fade logModal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="gridModalLabel">操作记录</h4>
            </div>
            <div class="modal-body" id="log-content">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
            <?= form_close() ?>
        </div>
    </div>
</div>

<!--  edit modal  -->
<div class="modal fade editModal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <?= form_open(site_url(''), Array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_edit')) ?>
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="gridModalLabel">编辑</h4>
            </div>
            <div class="modal-body">
                <div class="form-group"><label class="col-xs-3 control-label">商品：</label>
                    <div class="col-xs-7 product_name">

                    </div>
                </div>

                <div class="deliver-need" style="display: none;">
                    <?= edit_form_input('shipping_no', set_value('shipping_no'), '物流单号', 'readonly') ?>
                </div>
                <input type="hidden" name="is_deliver" value="0">
                <?= edit_form_input('order_no', set_value('order_no'), '订单号') ?>
                <?= edit_form_dropdown('shop_id', array('' => '选择账户组...') + $allShops, set_value('shop_id'), '选择账户组', "id='shop-id' style='width:100%;'") ?>
                <?= edit_form_dropdown('type_mark', array('' => '选择入库类型...') + IOSS_StaticConfig::getShippingType(), set_value('type_mark'), '入库类型', "id='type_mark'") ?>
                <?= edit_form_input('quantity', set_value('quantity'), '商品数量') ?>
                <?= edit_form_input('amount', set_value('amount'), '商品总价', 'required') ?>
                <?= edit_form_dropdown('location', array('' => '选择仓库...') + IOSS_StaticConfig::getShippingWarehouse(), set_value('location'), '仓库', "id='location'") ?>
                <?= edit_form_dropdown('area', array('' => '选择存储区...') + IOSS_StaticConfig::getShippingBlock(), set_value('area'), '所在区', "id='area'") ?>
                <div class="form-group">
                    <label class="col-xs-3 control-label">采购时间</label>
                    <div class="col-xs-7">
                        <div class="input-group input-large date-picker input-daterange" data-date=""
                             data-date-format="yyyy-mm-dd">
                            <input type="text" class="form-control" required name="purchase_time"
                                   value="<?= set_value('purchase_time') ?>" placeholder="采购时间">
                        </div>
                    </div>
                </div>
                <?= edit_form_textarea('remark', set_value('remark'), '备注信息') ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="submit" class="btn btn-primary ajaxSub">保存</button>
            </div>
            <?= form_close() ?>
        </div>
    </div>
</div>

<div class="modal fade in_stock" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <?= form_open(site_url('storage/warehouse/in_success'), Array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_edit')) ?>
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="gridModalLabel">确认入库</h4>
            </div>
            <div class="modal-body">

                <?= edit_form_dropdown('location', array('' => '选择存储仓库...') + IOSS_StaticConfig::getShippingWarehouse(), set_value('location'), '存储仓库', "id='location'") ?>
                <?= edit_form_dropdown('area', array('' => '选择仓库存储区...') + IOSS_StaticConfig::getShippingBlock(), set_value('area'), '仓库存储区', "id='area'") ?>
            </div>
            <input type="hidden" name="record_id">
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" onclick="ajaxPost()" class="btn btn-primary ajaxSub">保存</button>
            </div>
            <?= form_close() ?>
        </div>
    </div>
</div>

<!-- END PAGE HEADER-->
<script type="text/javascript">

    function confirmIn(id) {
        $('.in_stock').modal();
        $("input[name='record_id']").val(id);
    }

    function ajaxPost() {
        $(".ajaxSub").attr('disabled', 'disabled');
        var locations = $("#location").val();
        var area = $("#area").val();
        if (!locations || !area) {
            toastr.error('请选择存储仓库以及存储区！');
            return;
        }
        var id = $("input[name='record_id']").val();
        var ajaxData = {
            'id': id,
            'location': locations,
            'area': area
        };
        $.ajax({
            url: '<?= site_url('storage/warehouse/in_success') ?>',
            method: 'POST',
            data: ajaxData,
            dataType: 'json',
            success: function (res) {
                $(".ajaxSub").removeAttr('disabled');
                $('.in_stock').modal('hide');
                if (res.success) {
                    if (res.type == 'new') {
                        toastr.success(res.msg);
                    } else {
                        toastr.success('入库成功，当前库存已合并至所选批次！');
                    }
                } else {
                    toastr.error(res.msg);
                }
                setTimeout(function () {
                    location.reload();
                }, 1000)

            },
            fail: function (res) {
                console.log(res);
            }
        });
    }

    function defConfirm(url) {
        if (!confirm("作废后不可撤回，确定要作废吗？")) {
            return false;
        }
        window.location.href = url;
        return true;
    }

    $(document).ready(function () {
        $("#reset").click(function () {
            var SelectArr = $("select");
            var SelectShop = $("select[name='shop_id']");
            var SelectPurchaser = $("select[name='purchaser_id']");
            var SelectProduct = $("select[name='product_id']");
            $("input[type='text']").val('');
            SelectArr.val('');

            var shop = SelectShop[0].options[0].text;
            var Purchaser = SelectPurchaser[0].options[0].text;
            var product = SelectProduct[0].options[0].text;
            var v_span = $('.select2-chosen');
            v_span.eq(0).html(shop);
            v_span.eq(1).html(Purchaser);
            v_span.eq(2).html(product);

            for (var i = 0; i < SelectArr.length; i++) {
                var opt = SelectArr[i].options[0];
                opt.selected = true;

            }
        });
    });
</script>